package com.huluobo.a2207douyindemo.home;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.adapter.HomeVideoAdapter;
import com.huluobo.a2207douyindemo.databinding.FragmentHomeBinding;
import com.huluobo.a2207douyindemo.utils.ImageUtils;
import com.huluobo.base.mvvm.BaseMVVMFragment;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseMVVMFragment<FragmentHomeBinding, HomeViewModel> {

    private HomeVideoAdapter homeVideoAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initData() {
        vm.getHomeBanner();
        vm.getHomeVideos(1, 20);

        vm.getBannerSuccess.observe(this, bannerBeanResult -> {
            List<String> bannerList = new ArrayList<>();
            for (int i = 0; i < bannerBeanResult.getData().size(); i++) {
                bannerList.add(bannerBeanResult.getData().get(i).getImagePath());
            }
            binding.homeBanner.setImages(bannerList);
            binding.homeBanner.start();
        });

        vm.getBannerFailed.observe(this, s -> {
            Log.i("TagA", "获取首页banner失败 :" + s);
        });

        vm.getHomeVideoSuccess.observe(this, videoBeanResult -> {
            if (videoBeanResult.getCode() == 200) {
                homeVideoAdapter.getData().clear();
                homeVideoAdapter.getData().addAll(videoBeanResult.getData());
                homeVideoAdapter.notifyDataSetChanged();
            } else {
                ToastUtils.showLong(videoBeanResult.getMessage());
            }
        });

        vm.getHomeVideoFailed.observe(this, s -> {
            Log.i("TagA", "获取首页视频失败 :" + s);
        });

        vm.findVideoSuccess.observe(this, videoBeanResult -> {
            if (videoBeanResult.getCode()==200){
                homeVideoAdapter.getData().clear();
                homeVideoAdapter.getData().addAll(videoBeanResult.getData());
                homeVideoAdapter.notifyDataSetChanged();
            }
        });

        vm.findVideoFailed.observe(this, s -> {
            Log.i("TagA", "查找视频失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.homeBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                ImageUtils.getInstance().loadImage(context, path.toString(), imageView);
            }
        });

        binding.homeRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        homeVideoAdapter = new HomeVideoAdapter();
        binding.homeRv.setAdapter(homeVideoAdapter);

        binding.homeSearchBtn.setOnClickListener(v -> {
            String keyword = binding.homeSearchEt.getText().toString();
            if (keyword.isEmpty()) {
                ToastUtils.showLong("搜索内容不能为空");
                vm.getHomeVideos(1,20);
            } else {
                vm.findVideoByName(keyword);
            }
        });
    }

    @Override
    protected Class<HomeViewModel> getViewModel() {
        return HomeViewModel.class;
    }
}